<template>
  <div class="carousel-box" :style="{'--fontSize':btnSize+'px'}">
    <div class="carousel-img-box">
      <div  v-for="(item,index) in value" :key="item" :class="[computedImageClass(index),'img']" v-show="computedImageClass(index)" >
        <slot :item="item" :index="index">
 
        </slot>
      </div>
<!--      <img :src="item" v-for="(item,index) in value" :key="item" :class="computedImageClass(index)" v-show="computedImageClass(index)" alt="">-->
 
    </div>
    <button class="carousel-page-btn left" @click="last" ><</button>
    <button class="carousel-page-btn right" @click="next"> > </button>
 
    <div class="carousel-point-box" :style="{'--activePointColor':activePointColor,'--inactivePointColor':inactivePointColor}" >
      <div v-for="(item,index) in value.length" :class="[computedPointClass(index),'point']" @click="clickPoint(index)" :key="index" >
 
      </div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'Carousel',
  props:{
    value:{
      type:Array,
      default:[]
    },
    activePointColor:{
      type:String,
      default:"teal"
    },
    inactivePointColor:{
      type:String,
      default:"white"
    },
    autoPlay:{
      type:Boolean,
      default:false
    },
    duration:{
      type:Number,
      default:1000*2
    },
    btnSize:{
      type:Number,
      default:16
    }
 
 
  },
  data() {
    return {
      activeIndex:0,
 
    }
  },
  methods: {
 
    getIndex(num){
      const mod=this.value.length
      return (this.activeIndex+num+mod)%mod
    },
    last(){
      this.activeIndex=this.getIndex(-1)
    },
    next(){
      this.activeIndex=this.getIndex(1)
    },
    clickPoint(index){
      this.activeIndex=index
    }
  },
  computed:{
    computedPointClass(){
      return (index)=>{
        return this.activeIndex===index?'active-point':'inactive-point'
      }
    },
 
    computedImageClass(){
      return (index)=>{
 
        if( index === this.activeIndex){
          return 'center-img'
        }
        const left=this.getIndex(-1)
 
        if(index === left){
          return 'left-img'
        }
 
        const right=this.getIndex(1)
        if( index === right){
          return 'right-img'
        }
 
        return ''
 
      }
    }
  },
  mounted() {
    if(this.autoPlay){
      setInterval(()=>{
        this.next()
      },this.duration)
    }
 
  }
}
</script>
 
<style scoped>
  .carousel-box{
 
    position: relative;
    width: 100%;
    height: 100%;
    background-color:white;
 
  }
  .carousel-img-box{
    position: absolute;
    width: 100%;
    height:90%;
    display: flex;
    flex-direction: row;
    align-items: center;
 
 
  }
 
  .img{
    overflow: hidden;
    border: 1px solid #b8b7b7;
    transition: all 0.5s;
  }
  .left-img{
    position: absolute;
    left: 0;
    top: 10%;
    width: 50%;
    height: 80%;
    z-index: 1;
    opacity: 0.8;
 
 
  }
 
  .center-img{
    position: absolute;
    left: 20%;
    width: 60%;
    height: 100%;
    z-index: 2;
    background-color: white;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    transition: all 0.3s;
 
  }
  .right-img{
    position: absolute;
    left: 50%;
    top: 10%;
    width: 50%;
    height: 80%;
    z-index: 1;
    opacity: 0.8;
 
  }
  /*翻页按钮*/
  .carousel-page-btn{
    font-size: var(--fontSize);
    position: absolute;
    background-color: white;
    width: 2em;
    height: 2em;
    border-radius: 50%;
    border: 1px solid black;
    cursor: pointer;
    z-index: 4;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.5s;
 
  }
  /*左边翻页按钮*/
  .left{
 
    bottom:50%;
    left:0.5em ;
 
  }
  .left:hover{
    transform: translateX(-10%);
  }
  .right{
 
    bottom: 50%;
    right: 0.5em;
  }
  .right:hover{
    transform: translateX(10%);
  }
 
  .carousel-point-box{
 
    position: absolute;
    bottom:0;
    left: 0;
    width: 100%;
    height: 30px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
  .point{
    width: 10px;
    height: 10px;
    border-radius: 50%;
    border: 1px solid black;
    cursor: pointer;
    margin: 0 3px 0 3px;
    color: transparent;
 
  }
 
  /*爆红正常，不用管，可以正常使用*/
  .active-point{
    background-color: var(--activePointColor);
  }
  .inactive-point{
    background-color: var(--inactivePointColor);
  }
</style>
